//
//  CustomSheetView.m
//  CustomSheet
//
//  Created by Frayda on 10/2/17.
//  Copyright © 2017 72.iJF4Ever. All rights reserved.
//

#import "CustomSheetView.h"

#define kSheetButtonHeight  44.0f

@interface CustomSheetView()

@property (nonatomic,strong) UIView * contentView;
@property (nonatomic, strong) UIView *bgView;

@end

@implementation CustomSheetView

static NSArray * allbus = nil;
static NSInteger shouldBottomBtn = 0;
static float heigh4View = 0;

- (CustomSheetView*)initWithBottomButtonCount:(NSInteger)count otherButtonTitleArray:(NSArray *)titleArray
{
    allbus = titleArray;
    shouldBottomBtn = count;
    heigh4View = (kSheetButtonHeight*count + 10.0f) + kSheetButtonHeight * allbus.count;
    CustomSheetView *sheet = [[CustomSheetView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [sheet set];
    return  sheet;
}
- (void)set{
    [UIView animateWithDuration:0.25 animations:^{
        _contentView.frame = CGRectMake(0, kScreenHeight-heigh4View, kScreenWidth, heigh4View);
    }];
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _bgView.backgroundColor = [UIColor blackColor];
        _bgView.alpha = 0.4f;
        [self addSubview:_bgView];
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight,  kScreenWidth,heigh4View)];
        [self addSubview:_contentView];
        for (int i =0; i<allbus.count; i++) {
            UIButton * bu = [UIButton buttonWithType:UIButtonTypeCustom];
            bu.tag = i;
            bu.backgroundColor = [UIColor whiteColor];
            bu.frame = CGRectMake(0, kSheetButtonHeight*i, kScreenWidth, kSheetButtonHeight);
            bu.titleLabel.font = [UIFont systemFontOfSize:14.0f];
            [bu setTitle:allbus[i] forState:UIControlStateNormal];
            [bu setTitleColor:[UIColor colorWithHexString:@"354052"] forState:UIControlStateNormal];
            [bu addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
            [_contentView addSubview:bu];
            
            if (i != allbus.count - 1) {
                UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, kSheetButtonHeight - 0.5f, kScreenWidth, 0.5f)];
                line.backgroundColor = [UIColor colorWithHexString:@"e2e2e2"];
                [bu addSubview:line];
            }
        }
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, kSheetButtonHeight*allbus.count , kScreenWidth, 10.0f)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"ebf2f5"];
        [_contentView addSubview:lineView];
        
        if (shouldBottomBtn == 1) {
            UIButton *bottomBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, kSheetButtonHeight*allbus.count+10, kScreenWidth, kSheetButtonHeight)];
            bottomBtn.tag = 72;
            bottomBtn.backgroundColor = [UIColor whiteColor];
            [bottomBtn setTitle:@"取消" forState:UIControlStateNormal];
            bottomBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
            [bottomBtn setTitleColor:[UIColor colorWithHexString:@"a0abbf"] forState:UIControlStateNormal];
            [bottomBtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
            [_contentView addSubview:bottomBtn];
        }
    }
    return self;
}

- (void)createView
{
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _bgView.backgroundColor = [UIColor blackColor];
    _bgView.alpha = 0.4f;
    [self addSubview:_bgView];
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, heigh4View)];
    [self addSubview:_contentView];
    
    for (int i = 0; i < allbus.count; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.backgroundColor = [UIColor whiteColor];
        btn.frame = CGRectMake(0, kSheetButtonHeight * i, kScreenWidth, kSheetButtonHeight);
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [btn setTitle:allbus[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"354052"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:btn];
        
        if (i != (allbus.count - 1)) {
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, kSheetButtonHeight - 0.5f, kScreenWidth, 0.5f)];
            line.backgroundColor = [UIColor colorWithHexString:@"e2e2e2"];
            [btn addSubview:line];
        }
        
    }
    
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(0, kSheetButtonHeight * allbus.count, kScreenWidth, kSheetButtonHeight);
    [_contentView addSubview:lineView];
    
    if (shouldBottomBtn == 1) {
        
        UIButton *bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        bottomBtn.frame = CGRectMake(0, kSheetButtonHeight * allbus.count + 10, kScreenWidth, kSheetButtonHeight);
        bottomBtn.tag = 72;
        bottomBtn.backgroundColor = [UIColor whiteColor];
        [bottomBtn setTitle:@"取消" forState:UIControlStateNormal];
        bottomBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [bottomBtn setTitleColor:[UIColor colorWithHexString:@"a0abbf"] forState:UIControlStateNormal];
        [bottomBtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:bottomBtn];
    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [UIView animateWithDuration:0.25 animations:^{
        _contentView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, heigh4View);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
-(void)clickButton:(UIButton*)button{
    [self.delegate  actionSheetDidSelect:button.tag];
    [UIView animateWithDuration:0.25 animations:^{
        _contentView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, heigh4View);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
